﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.ApplyDraw
{
    public partial class Form : FormPage
    {
        BLL.Draw bll = new BLL.Draw();
        Model.Draw model = new Model.Draw();
        //
        int StoreID = 0, SupplierID = 0;
        BLL.Store bllStore = new BLL.Store();
        BLL.Supplier bllSupplier = new BLL.Supplier();
        Model.Store modelStore = new Model.Store();
        Model.Supplier modelSupplier = new Model.Supplier();
        //
        decimal accountCanBeDraw = 0;
        protected void Page_Load(object sender, EventArgs e) {
            StoreID = Security.GetCurrentStoreID();
            SupplierID = Security.GetCurrentSupplierID();
            if (StoreID > 0)
            {
                modelStore = bllStore.GetModel(StoreID);
                accountCanBeDraw = modelStore.MasterAccount - modelStore.FrozenAccount;
            }
            else if (SupplierID > 0)
            {
                modelSupplier = bllSupplier.GetModel(SupplierID);
                accountCanBeDraw = modelSupplier.MasterAccount - modelSupplier.FrozenAccount;
            }
            else Response.End();
        }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel()
        {
        }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            lb_AccountCanBeDraw.Text = StringPlus.showMoney(accountCanBeDraw);
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            decimal Money = DataTypePlus.requestDecimal(tb_Money.Text);

            if (
                Master.noZero(Money, "[提款金额]必须填写")&&
                Master.noErr(Money > accountCanBeDraw,"可提款余额不够")
                )
            {
                
                    model.StoreID = StoreID;
                    model.SupplierID = SupplierID;
                    model.Money = Money;
                    model.State = (int)EnumHelper.DrawState.Wait;
                    Master.id = bll.Add(model);
                    BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Draw, Master.id, EnumHelper.OperateType.Add, "提款申请" + StringPlus.showMoney(Money));
                    //冻结
                    CallBak<int> callBak = BLL.Account_Change_Log.Add(StoreID > 0 ? EnumHelper.UserType.Store : EnumHelper.UserType.Supplier, StoreID + SupplierID, EnumHelper.AccountChangeReason.ApplyDraw, Master.id, Money);
                    if (!callBak.IsSuccess)//失败
                    {
                        //更新失败状态
                        model = bll.GetModel(Master.id);
                        model.State = (int)EnumHelper.DrawState.NotAgree;
                        model.Description = callBak.ErrMsg;
                        bll.Update(model);
                        BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Draw, Master.id, EnumHelper.OperateType.Add, "提款申请失败:" + callBak.ErrMsg);
                        Master.ScriptManager_Alert_float(callBak.ErrMsg, false);
                    }
                    else
                    {
                        BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Account_Change_Log, callBak.ReturnVal, EnumHelper.OperateType.Add, "提款申请" + StringPlus.showMoney(Money));
                        Master.SubmitOK();
                    }
                
            }
        }
    }
}